/**
 * @author Liu Cong
 */

(function(){
	
const AMFException = FireAMF.Exception;

FireAMF.TypeLoader = {
    /**
     * @var array AMF class map
     */
    classMap: {
        'flex.messaging.messages.AcknowledgeMessage' : 'AcknowledgeMessage',
        'flex.messaging.messages.ErrorMessage'       : 'AsyncMessage',
        'flex.messaging.messages.CommandMessage'     : 'CommandMessage',
        'flex.messaging.messages.ErrorMessage'       : 'ErrorMessage',
        'flex.messaging.messages.RemotingMessage'    : 'RemotingMessage',
    },

    /**
     * @var array Default class map
     */
    _defaultClassMap: {
        'flex.messaging.messages.AcknowledgeMessage' : 'AcknowledgeMessage',
        'flex.messaging.messages.ErrorMessage'       : 'AsyncMessage',
        'flex.messaging.messages.CommandMessage'     : 'CommandMessage',
        'flex.messaging.messages.ErrorMessage'       : 'ErrorMessage',
        'flex.messaging.messages.RemotingMessage'    : 'RemotingMessage',
    },

    /**
     * Load the mapped class type into a callback.
     *
     * @param  string className
     * @return object|false
     */
    loadType: function(className) {
        var clazz    = false;
        var callBack = false;
        clazz    = this.getMappedClassName(className);
        if (!FireAMF[clazz]) {
            throw new AMFException(className +' mapped class '+ className + ' is not defined');
        }

        return FireAMF[clazz];
    },

    /**
     * Looks up the supplied call name to its mapped class name
     *
     * @param  string className
     * @return string
     */
    getMappedClassName: function(className) {
		for(var mappedName in this.classMap){
			if(this.classMap[mappedName] == className) return mappedName;
			else if(mappedName == className) return this.classMap[mappedName]; 
		}

        return false;
    },

    /**
     * Map PHP class names to ActionScript class names
     *
     * Allows users to map the class names of there action script classes
     * to the equivelent php class name. Used in deserialization to load a class
     * and serialiation to set the class name of the returned object.
     *
     * @param  string asClassName
     * @param  string jsClassName
     * @return void
     */
    setMapping: function(asClassName, jsClassName) {
        this.classMap[asClassName] = jsClassName;
    },

    /**
     * Reset type map
     *
     * @return void
     */
    resetMap: function() {
        this.classMap = this._defaultClassMap;
    }
};

})();
